<template>
  <div class="coupon-container">
    <van-nav-bar title="我的优惠券" left-text="返回" left-arrow @click-left="onClickLeft"/>
    <!-- 添加 Tab 切换 -->
    <van-tabs v-model:active="active" @change="onClickTab">
      <van-tab title="可使用" name="0"/>
      <van-tab title="已过期" name="1"/>
      <van-tab title="已使用" name="2"/>
    </van-tabs>

    <!-- 优惠券列表包装器 -->
    <div class="content-wrapper">
      <!-- 优惠券列表 -->
      <div class="coupon-list">
        <div class="coupon-div" v-for="item of coupons" :key="item.id">
          <div class="coupon-item">
            <div class="coupon-header">
              <span class="coupon-title">{{ item.coupon.name }}<span>【{{ item.userTypeName }}】</span></span>
              <span class="coupon-status">{{ item.userStatusName }}<span>【{{ item.overlayName }}】</span></span>
            </div>
            <div class="coupon-body">
              <p class="coupon-description"><span>
                <span
                    style="color: #CFAE00;font-size: 32px;font-weight: 600">满{{ item.couponCondition.condition }}￥
                </span>
                <span style="font-size: 20px;">
                  {{ item.typeName }}{{ item.couponCondition.faceValue }}
                </span>
              </span>
              </p>
              <p class="coupon-expire-date">有效期：{{ item.coupon.period }}{{ item.periodUnitName }}</p>
              <div>
                <span class="coupon-expire-date">领取时间：{{ item.createTime }}</span>&nbsp;&nbsp;
                <span class="coupon-expire-date">预计到期时间：{{ item.period }}</span>
              </div>
            </div>
          </div>
        </div>
      </div>

      <!-- 分页组件 -->
      <div class="pagination-wrapper" v-if="query.total > 0">
        <el-pagination 
          background
          layout="prev, pager, next"
          :current-page="query.pageNum"
          :page-size="query.pageSize"
          :total="query.total"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>

<script setup>
import {UserCouponList} from "@/api/market";
import {ref, onMounted} from "vue";
import router from "@/router";


// 分页参数
const query = ref({
  pageNum: 1,
  pageSize: 5,
  status: 0,
  total:0
})
// 保存状态码
let active = ref(0)
const coupons = ref([])

// 切换状态
function onClickTab() {
  query.value.status = active.value
  console.log(query.value.status)
  couponeList()
}

function handleCurrentChange(page){
  query.value.pageNum = page
  couponeList()
}

function couponeList() {
  UserCouponList(query.value).then(res => {
    console.log(res.data.data, +"------------------")
    if (res.data.code == 200000) {
      coupons.value = res.data.data.records
      query.value.total = res.data.data.total
      res.data.data.records.forEach(el => {
        // 初始化变量
        const moment = require('moment');
        // 现在时间
        var currentDate = el.createTime;
        // 使用moment解析并增加20天
        const newDate = moment(currentDate).add(el.coupon.period, 'days');
        // 格式化为原格式
        const newTimeStr = newDate.format('YYYY-MM-DD HH:mm:ss');
        el.period = newTimeStr
      })
    }
  })
}

function onClickLeft() {
  router.push("/mine/index")
}

onMounted(() => {
  couponeList() // 确保初始加载数据
})

</script>
<style scoped>

.expired .coupon-cell {
  background-color: #999;
  opacity: 0.7;
}

:deep(.van-cell) {
  background: transparent;
  padding: 0;
}

:deep(.van-cell__title) {
  flex: 1;
}

.coupon-div {
  width: 100%;
  padding: 0 15px;
  box-sizing: border-box;
  margin-top: 12px;
}

.coupon-item {
  border: none;
  border-radius: 12px;
  margin: 0 0 15px;
  padding: 15px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
  background: linear-gradient(135deg, #ff6b6b, #ff4757);
  position: relative;
  overflow: hidden;
}

.coupon-item::before {
  content: '';
  position: absolute;
  left: -5px;
  top: 50%;
  width: 10px;
  height: 10px;
  background: #f8f9fa;
  border-radius: 50%;
  transform: translateY(-50%);
}

.coupon-item::after {
  content: '';
  position: absolute;
  right: -5px;
  top: 50%;
  width: 10px;
  height: 10px;
  background: #f8f9fa;
  border-radius: 50%;
  transform: translateY(-50%);
}

.coupon-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-bottom: 10px;
  border-bottom: 1px dashed rgba(255, 255, 255, 0.3);
}

.coupon-title {
  font-size: 18px;
  font-weight: bold;
  color: white;
}

.coupon-title span {
  font-size: 14px;
  margin-left: 5px;
  opacity: 0.9;
}

.coupon-status {
  font-size: 14px;
  color: white;
  background: rgba(255, 255, 255, 0.2);
  padding: 4px 8px;
  border-radius: 4px;
}

.coupon-body {
  margin-top: 12px;
}

.coupon-description {
  margin: 10px 0;
}

.coupon-description span {
  display: flex;
  align-items: baseline;
}

.coupon-expire-date {
  font-size: 12px;
  color: rgba(255, 255, 255, 0.9);
  margin-top: 8px;
}

/* 分页样式优化 */
.example-pagination-block {
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 0;
  padding: 10px 0;
}

:deep(.el-pagination) {
  --el-pagination-hover-color: #ff4757;
  --el-pagination-button-color: #606266;
  --el-pagination-button-bg-color: #fff;
  --el-pagination-button-disabled-color: #c0c4cc;
  --el-pagination-button-disabled-bg-color: #fff;
  --el-pagination-hover-bg-color: #f4f4f5;
}

/* Tab样式优化 */
:deep(.van-tabs) {
  --van-tabs-bottom-bar-color: #ff4757;
  --van-tabs-nav-background-color: #ffffff;
}

:deep(.van-tab) {
  color: #666;
}

:deep(.van-tab--active) {
  color: #ff4757;
  font-weight: bold;
}

/* 添加容器样式 */
.coupon-container {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  background-color: #f5f5f5;
}

.content-wrapper {
  flex: 1;
  display: flex;
  flex-direction: column;
  position: relative;
  padding-bottom: 60px; /* 为分页留出空间 */
}

.coupon-list {
  flex: 1;
}

.pagination-wrapper {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 15px 0;
  background-color: #fff;
  box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.05);
  z-index: 10;
  display: flex;
  justify-content: center;
}

/* 确保分页器可见 */
:deep(.el-pagination) {
  display: flex;
  justify-content: center;
}

:deep(.el-pagination .el-pager li) {
  background-color: #fff;
  color: #606266;
}

:deep(.el-pagination .el-pager li.active) {
  background-color: #ff4757;
  color: #fff;
}

</style>